package reader;

import java.io.FileReader;

/**
 * FileReader类成员方法:
 *      public int read(char[] cbuf) throws IOException
 *          从文件中尽可能多的读取字符到cbuf字符数组中(每次最多不超过cubf.length)
 *          返回值为每次从文件中读取到的字符的个数
 *  String类中的转换方法:
 *      public String(char[] value):将一个字符数组中的所有元素转成一个字符串
 *      public String(char[] value, int offset, int count) :将字符数组的offset索引开始count个字符(包含offset索引的字符)转成字符串
 */
public class FileReaderDemo02 {
    public static void main(String[] args)throws Exception {
        //1.关联文件
        FileReader fr = new FileReader("day23-IO-02/6.txt");

        //2.定义一个字符数组
        char[] chars = new char[2];

/*        //3.利用read()方法将字符读取到字符数组中
        int length = fr.read(chars);
        System.out.println(length + " " + new String(chars));//2 "手表" chars={'手','表'}

        int length02 = fr.read(chars);
        System.out.println(length02 + " " + new String(chars));//2 "电动" chars={'电','动'}

        int length03 = fr.read(chars);
        System.out.println(length03 + " " + new String(chars));//1 "车动" chars={'车','动'}

        int length04 = fr.read(chars);
        System.out.println(length04 + " " + new String(chars));//-1 "车动" chars={'车','动'}*/

        int length;
        while ((length = fr.read(chars)) != -1) {
            System.out.println(length + " " + new String(chars, 0, length));
        }

        fr.close();

    }
}
